AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:Initialize()
	self.Entity:SetModel("models/lk_models/lk_monitor.mdl") 
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)
	
	self.minelevation = -25
	self.maxelevation = 25
	self.elevation = 0
	self.aimspeed = 0.25
	self.CanFire = CurTime()
	
	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then phy:Wake() end
	
	self.Entity:ResetSequence(1)
	self.Entity:SetPlaybackRate(0)
	self.Entity:SetCycle(0.4)
end

function ENT:SpawnFunction( ply, tr )

	local price = 800
	
	if(tonumber(ply.money) < price or ply.turrets>=6) then 
	return false
	else
	ply.money = ply.money-price
	MoneyHUDUpdate( ply )
	SavePlayerData( ply )
	ply.turrets = ply.turrets+1
	end

	local ent = ents.Create( "lk_turret_mount" )
	local SpawnPos = tr.HitPos + tr.HitNormal * 32
	ent:SetModel("models/hunter/blocks/cube3x3x05.mdl")
	ent:SetPos( SpawnPos )
	ent:SetAngles( Angle(0,tr.Normal:Angle().y,0) )
	ent:Activate()
	ent.ply = ply
	ent:Spawn()
	
	return ent
end

function ENT:Think()

if( !self.base:IsValid() ) then
self.Entity:Remove()
end

self.elevation = math.Clamp(self.base.gunpitch,self.minelevation,self.maxelevation)
local range = self.maxelevation-self.minelevation
local deg = math.Clamp(self.elevation-self.minelevation,0,range)
local frame = deg/range

if( self.base.core.turretfire and self.CanFire<CurTime() ) then

	local ent = ents.Create( "lk_bullet" )
	local Pos = Vector(58,0,20)
	local Norm = Vector(1,0,0)
	local FirePos = Vector(100,-18,0)
	local Ang = Angle(-self.elevation,0,0)
	Norm:Rotate( Ang )
	FirePos:Rotate( Ang )
	local bulletPos = self.Entity:LocalToWorld(Pos + FirePos)
	ent:SetPos( bulletPos )
	ent:Activate()
	ent:Spawn()
	ent.core = self.base.core
	ent.Vel = self.Entity:LocalToWorld(Norm*200)-self.Entity:GetPos()
	ent:SetAngles( ent.Vel:Angle() )
	ent.gravity = 0.1
	ent.damage = 600
	ent.dietime = CurTime()+0.75
	ent.ExpScale = 2
	local effectdata = EffectData()
	effectdata:SetOrigin( bulletPos )
	effectdata:SetNormal( ent.Vel:GetNormalized() )
	effectdata:SetScale( 3 )
	util.Effect( "lk_fireeffect" , effectdata )
	
	local ent = ents.Create( "lk_bullet" )
	local FirePos = Vector(100,18,0)
	FirePos:Rotate( Ang )
	local bulletPos = self.Entity:LocalToWorld(Pos + FirePos)
	ent:SetPos( bulletPos )
	ent:Activate()
	ent:Spawn()
	ent.core = self.base.core
	ent.Vel = self.Entity:LocalToWorld(Norm*200)-self.Entity:GetPos()
	ent:SetAngles( ent.Vel:Angle() )
	ent.gravity = 0.25
	ent.damage = 600
	ent.dietime = CurTime()+0.75
	ent.ExpScale = 2
	local effectdata = EffectData()
	effectdata:SetOrigin( bulletPos )
	effectdata:SetNormal( ent.Vel:GetNormalized() )
	effectdata:SetScale( 3 )
	util.Effect( "lk_fireeffect" , effectdata )
	
	self.Entity:EmitSound("medturret_fire.wav",150,80)

	self.CanFire = CurTime()+6
end

self.Entity:ResetSequence(1)
self.Entity:SetPlaybackRate(0)
self.Entity:SetCycle(math.Clamp(frame,0,1))

self.Entity:NextThink( CurTime() + 0.05 )
return true

end

